﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using Sonce.Common;

namespace Sonce.Controls
{
    public partial class LoadingAnimation : System.Windows.Controls.Control
    {
        #region Members
        private bool isEnabled = true;
        public const double SpeedRatio = 0.7;
        #endregion

        #region Properties
        /// <summary>
        /// Is loading animation running?
        /// </summary>
        public new bool IsEnabled
        {
            get
            {
                return this.isEnabled;
            }
            set
            {
                this.isEnabled = value;
                if (value)
                {
                    //TODO: Fade in
                    changeState(loadingState);
                    this.Visibility = Visibility.Visible;
                }
                else
                {
                    //TODO: Fade out
                    changeState(normalState);
                    this.Visibility = Visibility.Collapsed;
                }
            }
        }
        #endregion

        #region Constructor
        public LoadingAnimation()
        {
            //this.DefaultStyleKey = typeof(LoadingAnimation);
            this.Loaded += new RoutedEventHandler(Control_Loaded);
        }
        #endregion

        #region Methods
        private void changeState(Storyboard state)
        {
            if (state != null)
            {
                state.Begin();
            }
            if (currentState != null)
            {
                currentState.Stop();
            }
            currentState = state;
        }
        #endregion

        #region Controls
        private FrameworkElement RootElement;
        private Canvas pnlLoadingAnimation;
        private Storyboard normalState;
        private Storyboard loadingState;
        private Storyboard currentState;

        public override void OnApplyTemplate()
        {
            RootElement = (FrameworkElement)GetTemplateChild("RootElement");
            pnlLoadingAnimation = (Canvas)GetTemplateChild("LoadingAnimation");
            normalState = (Storyboard)pnlLoadingAnimation.Resources["Normal State"];
            loadingState = (Storyboard)pnlLoadingAnimation.Resources["Loading State"];
            this.IsEnabled = this.isEnabled;
        }

        private void Control_Loaded(object sender, RoutedEventArgs e)
        {
            Loader.LoadStyle(this);
        }
        #endregion

        #region Static methods
        public static void SetAnimation(Panel panel, bool show)
        {
            //Find existing loading animation
            LoadingAnimation anim = null;
            foreach (UIElement element in panel.Children)
            {
                if (element is LoadingAnimation)
                {
                    anim = element as LoadingAnimation;
                    break;
                }
            }

            //Attach to panel
            if (show && anim == null)
            {
                anim = new LoadingAnimation();
                anim.Width = panel.Width;
                anim.Height = panel.Height;
                panel.Children.Add(anim);
                Canvas.SetZIndex(anim, 100);
                Grid.SetColumnSpan(anim, 10);
                Grid.SetRowSpan(anim, 10);
            }

            //Start or stop animation
            if (anim != null)
            {
                anim.IsEnabled = show;
                if (!show)
                {
                    ((Panel)anim.Parent).Children.Remove(anim);
                }
            }
        }
        #endregion
    }
}
